// pages/goodsAddress/goodsAddress.js
const http = require('../../utils/http.js');
const app = getApp();
const { $Toast } = require('../../dist/base/index');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    
  },

  //获取地址
  getAddress(){
    var uid = app.globalData.uid;
    http.fetchApi('procedure_t', { p: 'R2003008|' + uid }).then(res=>{
      if(res.data.code==1){
        var addressArr = res.data.results;
        if (addressArr.length == 0) {
          wx.removeStorageSync('nowAddress')
        }else{
          if (addressArr[0].codes == 0) {
            $Toast({
              content: '服务器好像出错啦',
              type: 'error'
            });
          }
        }
        this.setData({
          addressArr
        })
       
        
        
        
      }else{
       
        $Toast({
          content: '服务器好像出错啦',
          type: 'error'
        });
      }
    })
  },

//添加收货地址
  addAddress(){
    wx.navigateTo({
      url: '../addAddress/addAddress?type=1',
    })
  },

  //编辑地址
  editAddress(e){
    var address_id=e.currentTarget.dataset.id;
    console.log(address_id)
    wx.navigateTo({
      url: '../addAddress/addAddress?address_id=' + address_id+"&type=0",
    })
  },

//删除地址
  deleteAddress(e){
    var address_id = e.currentTarget.dataset.id;
    var nowAddre_id = wx.getStorageSync('nowAddress').id || '';
    
    console.log(address_id)
    http.showModal('确定要删除?').then((res=>{
      if (res.confirm) {
        http.fetchApi('procedure_t', { p: `R2003009|${address_id}`  }).then(res=>{
          if (res.data.code == 1) {

            if (address_id == nowAddre_id) {
             // console.log('正在删除选中的学校')
              wx.removeStorageSync('nowAddress');
            }

            $Toast({
              content: '删除成功',
              type: 'success'
            });
            setTimeout(() => { this.getAddress() }, 500)
           
          }
        })
      } else if (res.cancel) {
        console.log('用户点击取消')
      }
    }))
  },

//下单时选择收货地址
  selectAddress(e){
    if (this.data.prevPage =='takeExpress'){
      var item = e.currentTarget.dataset.item;
      wx.setStorageSync('nowAddress', item);
      wx.navigateBack({

      })
    }else{
      return
    }
   
  },

  //设置默认地址
  setDefaultAddress(e){
    var uid = app.globalData.uid;
    var address_id = e.currentTarget.dataset.id;
    http.fetchApi('procedure_t', { p: `R2005004|${uid}|${address_id}` }).then(res=>{
      if(res.data.code==1){

        $Toast({
          content: '设置默认地址成功',
          type: 'success'
        });
        setTimeout(() => { this.getAddress() }, 500)
       
      }
    })
  },
  

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var prevPage=options.page;
    console.log(prevPage);
    this.setData({
      prevPage
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getAddress();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})